//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "Input.h"
#include "Pattern.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TInputForm *InputForm;
//---------------------------------------------------------------------------
__fastcall TInputForm::TInputForm(TComponent* Owner)
	: TForm(Owner)
{
	bPressed = false;
    bEndRecognize = false;
    result = FALSE;
    bInputed = false;
    counter = 0;

    Canvas->Pen->Color = clBlack;
    Canvas->Pen->Width = 2;
}
//---------------------------------------------------------------------------
void __fastcall TInputForm::FormMouseDown(TObject *Sender, TMouseButton Button,
	TShiftState Shift, int X, int Y)
{
    bInputed = true;
	bPressed = true;
    ox = X;
    oy = Y;
    pattern.Add(X,Y);
}
//---------------------------------------------------------------------------
void __fastcall TInputForm::FormMouseMove(TObject *Sender, TShiftState Shift,
	int X, int Y)
{
	if(bPressed) {
    	Canvas->MoveTo(ox,oy);
        Canvas->LineTo(X,Y);
        ox = X;
        oy = Y;
	    pattern.Add(X,Y);
    }
}
//---------------------------------------------------------------------------
void __fastcall TInputForm::FormMouseUp(TObject *Sender, TMouseButton Button,
	TShiftState Shift, int X, int Y)
{
	bPressed = false;
	if(pattern.point!=0&&(pattern.GetX(pattern.point-1)!=0
    		||pattern.GetY(pattern.point-1)!=0)){
    	pattern.Add(0,0);
    }

}
//---------------------------------------------------------------------------

int __fastcall TInputForm::GetResult(void)
{
	if(bPressed) return FALSE;
    if(!bInputed) return FALSE;
    if(!bEndRecognize) return FALSE;

    bEndRecognize = false;
    ClearScreen();
    pattern.Clear();
	bInputed = false;
  	return result;

}
//---------------------------------------------------------------------------
void __fastcall TInputForm::Timer1Timer(TObject *Sender)
{
	if(bPressed) return;
    if(!bInputed) return;
    if(bEndRecognize) return;

    counter ++;
    Edit1->Text = counter;
    if(counter<30) return;
    else counter = 0;
    Edit1->Text = counter;

	result = recognize.DoRecognize(&pattern);
    bEndRecognize = true;
}
//---------------------------------------------------------------------------

void TInputForm::ClearScreen(void)
{
    Canvas->Pen->Color = clWhite;
    Canvas->Rectangle(0,0,Width,Height);
    Canvas->Pen->Width = 2;

    Canvas->Pen->Color = clBlack;
    Canvas->Pen->Width = 2;
}
